package org.dreamwork.jasmine2.web.controls.chart;

import org.dreamwork.jasmine2.annotation.WebControlTemplate;
import org.dreamwork.jasmine2.util.StringConverter;
import org.dreamwork.jasmine2.web.controls.chart.format.IChartDataFormat;
import org.dreamwork.jasmine2.web.controls.chart.format.PieDataFormat;
import org.dreamwork.jasmine2.web.controls.chart.settings.ChartSetting;
import org.dreamwork.jasmine2.web.controls.chart.settings.ChartSettingSection;
import org.dreamwork.jasmine2.web.controls.chart.settings.ChartType;
import org.dreamwork.jasmine2.web.controls.chart.settings.PieChartSetting;
import org.dreamwork.jasmine2.web.controls.chart.settings.template.PieChartTemplate;
import org.dreamwork.peony.DataBinder;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2010-5-12
 * Time: 10:37:38
 */
@WebControlTemplate (PieChartTemplate.class)
public class PieChart extends AbstractChartControl {
    private String colorField;
    private boolean use3D = true;
    private int height3D = 30;
    private boolean legendEnabled = true;

    public PieChart () {
        title = "Pie Chart";
    }

    public boolean isUse3D () {
        return use3D;
    }

    public void setUse3D (boolean use3D) {
        this.use3D = use3D;
    }

    public int getHeight3D () {
        return height3D;
    }

    public void setHeight3D (int height3D) {
        this.height3D = height3D;
    }

    public boolean isLegendEnabled () {
        return legendEnabled;
    }

    public void setLegendEnabled (boolean legendEnabled) {
        this.legendEnabled = legendEnabled;
    }

    public String getColorField () {
        return colorField;
    }

    public void setColorField (String colorField) {
        this.colorField = colorField;
    }

    protected String parseDataAsXML () {
        IChartDataFormat cdf = new PieDataFormat ();
        ChartContext ctx = new ChartContext ();
        ctx.addParameter ("textField", textField);
        ctx.addParameter ("valueField", valueField);
        if (!StringConverter.isEmpty (colorField))
            ctx.addParameter ("colorField", colorField);
        if (!StringConverter.isEmpty (url))
            ctx.addParameter ("url", url);

        List<?> data = DataBinder.parseDataSource (dataSource);
        return cdf.format (data, ctx);
    }

    @Override
    protected String getSWFName () {
        return "pie";
    }

    @Override
    protected ChartSetting getSetting () {
        PieChartSetting setting = new PieChartSetting ();
        ChartSettingSection pie =
                setting.addSection ("pie").addAttribute ("angle", "30").addAttribute ("hover_brightness", "20");
        if(use3D) pie.addAttribute ("height", String.valueOf (height3D));
        setting.setLegendEnabled (legendEnabled);
        return setting;
    }

    @Override
    protected ChartType getChartType () {
        return ChartType.Pie;
    }

    @Override
    @SuppressWarnings ("unchecked")
    protected String getServletParameters () {
        StringBuilder builder = new StringBuilder ();
        builder.append ("r=").append (getChartType ());
        builder.append ("&p=").append (getProviderKey ());
        builder.append ("&v=").append (valueField);
        builder.append ("&t=").append (textField);
        if (!StringConverter.isEmpty (colorField)) builder.append ("&c=").append (colorField);
        if (!StringConverter.isEmpty (url)) builder.append ("&u=").append (url);

        HttpServletRequest request = page.getHttpContext ().getRequest ();
        for (Enumeration e = request.getParameterNames (); e.hasMoreElements ();) {
            String key = (String) e.nextElement ();
            if (key.startsWith ("__")) continue;
            String[] values = request.getParameterValues (key);
            if (values.length > 1) {
                for (String v : values) builder.append ('&').append (key).append ('=').append (v);
            } else {
                builder.append ('&').append (key).append ('=').append (values [0]);
            }
        }
        return builder.toString ();
    }
}